package com.whg.shiro.controller;

import com.whg.shiro.domain.SimpleResult;
import com.whg.shiro.domain.UserModel;
import com.whg.shiro.domain.common.JsonData;
import com.whg.shiro.domain.dto.UserQuery;
import com.whg.shiro.domain.vo.UserRolesVO;
import com.whg.shiro.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author whg
 * @create 2022/9/28 17:10
 * @description：
 */
@RestController
@RequestMapping("/pub")
public class PublicController {

    @Resource
    private UserService userService;

    @RequestMapping("/findUserById")
    public SimpleResult findUserById(@RequestParam("id") int id){
        SimpleResult result = new SimpleResult();
        UserModel userModel = userService.findUserById(id);
        result.setData(userModel);
        return result;
    }

    @RequestMapping("/findUserByName")
    public SimpleResult findUserByName(@RequestParam("name") String name){
        SimpleResult result = new SimpleResult();
        UserModel userModel = userService.findUserByName(name);
        result.setData(userModel);
        return result;
    }

    @RequestMapping("/findUserRolesByName")
    public SimpleResult findUserRolesByName(@RequestParam("name") String name){
        SimpleResult result = new SimpleResult();
        UserRolesVO vo = userService.findUserRolesByUserName(name);
        result.setData(vo);
        return result;
    }

    @RequestMapping("need_login")
    public JsonData needLogin(){
        return JsonData.buildError("温馨提示：请使用对应的账号登录",-2);
    }

    @RequestMapping("not_permit")
    public JsonData netPermit(){
        return JsonData.buildError("温馨提示：拒绝访问，没权限",-3);
    }

    @RequestMapping("index")
    public JsonData index(){
        List<String> videoList = new ArrayList<>();
        videoList.add("java");
        videoList.add("springboot");
        videoList.add("linux");
        videoList.add("mysql");
        videoList.add("shiro");
        return JsonData.buildSuccess(videoList);
    }

    @PostMapping("login")
    public JsonData login(@RequestBody UserQuery userQuery,
                          HttpServletRequest request,
                          HttpServletResponse response){
        Subject subject = SecurityUtils.getSubject();
        try{
            Map<String,Object> map = new HashMap<>();
            UsernamePasswordToken token = new UsernamePasswordToken(
                    userQuery.getUserName(),
                    userQuery.getPassword());
            subject.login(token);
            map.put("msg","登录成功");
            map.put("sessionId",subject.getSession().getId());
            return JsonData.buildSuccess(map);
        }catch (Exception e){
            e.printStackTrace();
            return JsonData.buildError("账户或密码错误");
        }
    }

    @PostMapping("register")
    public JsonData login(@RequestBody UserQuery userQuery){
        userService.register(userQuery);
        return JsonData.buildSuccess();
    }


}
